* Set working directory to "PNAS_Replication/" folder
cd "~/Dropbox/PNAS_Replication"
* install mat2txt and mat2tex
** net install mat2tex, from(https://raw.githubusercontent.com/avila/mat2tex/master/)
** ssc install mat2txt

********************************************************************************
*** Figure 1: Composition of Hate Crime Supporters by Gender, Age and Income ***
********************************************************************************
use "Data/for_dataverse.dta", clear
keep if focus == 1

set scheme s1mono
label def female 1 "Female" 0 "Male"
label val female female

label define age_group 1 "18-29" 2 "30-39" 3 "40-49" 4 "50-59" 5 "60+"
label val age_group age_group
 
keep if inlist(wave, 2, 3) | (wave == 4 & first_row_wave_4 == 1)
gen old = age
replace old = 1 if age>45 & age!=.
replace old = 0 if age<=45 & age!=.

gen young = old
recode young 1=0 0=1 

gen atabove30 = age
recode atabove30 18/29=0 30/99=1

gen below30 = atabove30
recode below30 1=0 0=1

gen atabove40 = age
recode atabove40 18/39=0 40/99=1

gen below40 = atabove40
recode below40 1=0 0=1

sum income, d
gen above_med_income = income
replace above_med_income = 1 if income>5 & income!=.
replace above_med_income = 0 if income<=5 & income!=.

gen atbelow_med_income = above_med_income
recode atbelow_med_income 1=0 0=1

gen atabove_med_income = income
replace atabove_med_income = 1 if income>=5 & income!=.
replace atabove_med_income = 0 if income<5

gen below_med_income = atabove_med_income
recode below_med_income 1=0 0=1

gen male = female
recode male 1=0 0=1

foreach var in female male old young atabove30 below30 atabove40 below40 above_med_income atbelow_med_income  atabove_med_income below_med_income {
   gen x100_`var' = `var'*100
}

gen zero = 0
gen zero2 = 0
graph bar x100_male x100_female zero x100_below40 x100_atabove40 zero2 x100_below_med_income x100_atabove_med_income  if dum_hate_pol_message==1, ///
scale(*.8) ///
bargap(5) legend(off) ylab(0(10)70, nogrid) ///
bar(1, color(gs3)) bar(2, color(gs7)) bar(3, color(gs3)) ///
bar(4, color(gs7)) bar(5, color(gs3)) bar(6, color(gs7)) /// 
bar(7, color(gs3)) bar(8, color(gs3))  ///
yline(50, lcolor(red) lpattern(dash))  ytitle(Composition of Hate Crime Supporters (%), height(5)) ///
showyvars yvaroptions(relabel(1 "Male" 2 "Female" 3 "" 4 "Below 40" 5 "40 and Above" 6 "" 7 "Low" 8 "Med-High")) ///
text(-4.5 14 "Gender", size(11pt)) text(-4.5 50 "Age", size(11pt)) text(-4.5 86 "Income", size(11pt)) ///
plotregion(lstyle(none))
graph export "Results/Figure_1.pdf", replace

*** In the paper, the formatting of Figure1.pdf above was manually modified using the Graph Editor, by:
*** (1) Playing "grec_files/add_blank_labels.grec" file 
*** (2) Saving modified plot as Results/Figure_1.pdf


********************************************************************************
************************* Table 1: Hate Crime Support **************************
********************************************************************************
use "Data/for_dataverse.dta", clear
keep if focus == 1
keep if inlist(wave, 2, 3) | (wave == 4 & first_row_wave_4 == 1)
mat prop_support = .
mat colnames prop_support = "proportion"
foreach var in "violence_means" "pol_message" "justified" "prevent_settlement"   {	
	summ dum_hate_`var'
	mat prop_support = prop_support \ (`r(mean)' * 100)
}
mat rownames prop_support = . "Only Means" "Homes" "Justified" "Settlement" 
mat2tex using "Results/Table_1.tex", matrix(prop_support) replace format(%9.2fc)


********************************************************************************
********** Figure 2: Anti-Refugee Sentiment and Hate Crime Support *************
********************************************************************************
use "Data/for_dataverse.dta", clear
keep if focus == 1
keep if inlist(wave, 2, 3) | (wave == 4 & first_row_wave_4 == 1)
gen graph_hate_pol_message = hate_pol_message
label define lab_hate_pol_message 1 "Homes: Strongly disagree" 2 "Homes: Somewhat disagree" 3 "Homes: Somewhat agree" 4 "Homes: Strongly agree"
label values graph_hate_pol_message lab_hate_pol_message

hist mean8_ref, by(graph_hate_pol_message, yrescale rows(2) ixtitles note("")) discrete xtitle("Anti-refugee sentiment") freq name(ref_by4_message, replace) title("")
graph export "Results/Figure_2.pdf", replace
*** In the paper, the formatting of Figure2.pdf above was manually modified using the Graph Editor, by:
  *** (1) Playing "grec_files/remove_overall_x_title" 
  *** (2) Saving as "Results/Figure2.pdf"


********************************************************************************
****************** Figure 3: Hate Crime Support by Group ***********************
********************************************************************************
use "Data/for_dataverse.dta", clear
keep if focus == 1
keep if inlist(wave, 2, 3) | (wave == 4 & first_row_wave_4 == 1)
svyset pseudonym
global prefix ""

label def female 1 "Female" 0 "Male"
label val female female

label define age_group 1 "18-29" 2 "30-39" 3 "40-49" 4 "50-59" 5 "60+"
label val age_group age_group 

* Gender
local jitter = 1
foreach var in "pol_message" "prevent_settlement" "justified" "violence_means" {
	mat m_`var'_female = (., ., ., .) \ (., ., ., .) \ (., ., ., .) \ (., ., ., .)  // adding 3 extra blank rows so this matrix can conform with the others (which have 5 levels instead of 2)
	mat colnames m_`var'_female = "`var'_female" "`var'_female_est" "`var'_female_LL" "`var'_female_UL"
	forvalues i=0/1 {
		$prefix mean dum_hate_`var' if female == `i'	
		mat a = r(table)
		mat m_`var'_female = m_`var'_female \ ((1 + `i' - 0.10 + 0.05 * `jitter'), a["b", 1], a["ll", 1], a["ul", 1])   
	}
	local ++jitter
	di `jitter'
}		
mat m_female = m_pol_message_female, m_prevent_settlement_female, m_justified_female, m_violence_means_female	
	
* Age
   local jitter = 1
foreach var in "pol_message" "prevent_settlement" "justified" "violence_means" {
	mat m_`var'_age_group = ., ., ., .
	mat colnames m_`var'_age_group = "`var'_age_group" "`var'_age_group_est" "`var'_age_group_LL" "`var'_age_group_UL"
	forvalues i=1/5 {
		$prefix mean dum_hate_`var' if age_group == `i'	
		mat a = r(table)
		mat m_`var'_age_group = m_`var'_age_group \ ((`i' - 0.25 + 0.15 * `jitter'), a["b", 1], a["ll", 1], a["ul", 1])   
	}
	local ++jitter
	di `jitter'
}		
mat m_age_group = m_pol_message_age_group, m_prevent_settlement_age_group, m_justified_age_group, m_violence_means_age_group					
		
*Income
local jitter = 1
foreach var in "pol_message" "prevent_settlement" "justified" "violence_means" {
	mat m_`var'_income = ., ., ., .
	mat colnames m_`var'_income = "`var'_income" "`var'_income_est" "`var'_income_LL" "`var'_income_UL"
	forvalues i=1/5 {
		$prefix mean dum_hate_`var' if quint_income == `i'	
		mat a = r(table)
		mat m_`var'_income = m_`var'_income \ ((`i' - 0.25 + 0.15 * `jitter'), a["b", 1], a["ll", 1], a["ul", 1])   
	}
	local ++jitter
	di `jitter'
}		
mat m_income = m_pol_message_income, m_prevent_settlement_income, m_justified_income, m_violence_means_income		
	
* Education 
local jitter = 1
foreach var in "pol_message" "prevent_settlement" "justified" "violence_means" {
	mat m_`var'_edu = ., ., ., .
	mat colnames m_`var'_edu = "`var'_edu" "`var'_edu_est" "`var'_edu_LL" "`var'_edu_UL"
	forvalues i=1/5 {
		$prefix mean dum_hate_`var' if quint_eduyrs == `i'	
		mat a = r(table)
		mat m_`var'_edu = m_`var'_edu \ ((`i' - 0.25 + 0.15 * `jitter'), a["b", 1], a["ll", 1], a["ul", 1])   
	}
	local ++jitter
	di `jitter'
}		
mat m_edu = m_pol_message_edu, m_prevent_settlement_edu, m_justified_edu, m_violence_means_edu		
		
*Education by young male
local jitter = 1
foreach var in "pol_message" "prevent_settlement" "justified" "violence_means" {
	mat m_`var'_edu_ym = ., ., ., .
	mat colnames m_`var'_edu_ym = "`var'_edu_ym" "`var'_edu_ym_est" "`var'_edu_ym_LL" "`var'_edu_ym_UL"
	forvalues i=1/5 {
		$prefix mean dum_hate_`var' if quint_eduyrs == `i'	& female==0 & age_group<3
		mat a = r(table)
		mat m_`var'_edu_ym = m_`var'_edu_ym \ ((`i' - 0.25 + 0.15 * `jitter'), a["b", 1], a["ll", 1], a["ul", 1])   
	}
	local ++jitter
	di `jitter'
}		
mat m_edu_ym = m_pol_message_edu_ym, m_prevent_settlement_edu_ym, m_justified_edu_ym, m_violence_means_edu_ym		
	
*Education by young female
local jitter = 1
foreach var in "pol_message" "prevent_settlement" "justified" "violence_means" {
	mat m_`var'_edu_yf = ., ., ., .
	mat colnames m_`var'_edu_yf = "`var'_edu_yf" "`var'_edu_yf_est" "`var'_edu_yf_LL" "`var'_edu_yf_UL"
	forvalues i=1/5 {
		$prefix mean dum_hate_`var' if quint_eduyrs == `i'	& female==1 & age_group<3
		mat a = r(table)
		mat m_`var'_edu_yf = m_`var'_edu_yf \ ((`i' - 0.25 + 0.15 * `jitter'), a["b", 1], a["ll", 1], a["ul", 1])   
	}
	local ++jitter
	di `jitter'
}		
mat m_edu_yf = m_pol_message_edu_yf, m_prevent_settlement_edu_yf, m_justified_edu_yf, m_violence_means_edu_yf		
	
* combine all datasets for ci plots
mat m_demographics = m_female, m_age_group, m_income, m_edu, m_edu_ym, m_edu_yf	
drop _all
svmat m_demographics, names(col)
drop if _n == 1	
	
* make ci plots
** female
twoway (scatter pol_message_female_est pol_message_female, msymbol(0) mcolor(gs0))   ///
	   (scatter prevent_settlement_female_est prevent_settlement_female, msymbol(Oh) mcolor(red))  ///
	   (scatter justified_female_est justified_female, msymbol(S) mcolor(gs0))  ///
	   (scatter violence_means_female_est violence_means_female, msymbol(Sh) mcolor(red))  ///
	   (rcap pol_message_female_UL pol_message_female_LL pol_message_female, lstyle(ci) msize(vtiny))  ///
	   (rcap prevent_settlement_female_UL prevent_settlement_female_LL prevent_settlement_female, lstyle(ci) msize(vtiny))   ///
       (rcap justified_female_UL justified_female_LL justified_female, lstyle(ci) msize(vtiny))   ///
	   (rcap violence_means_female_UL violence_means_female_LL violence_means_female, lstyle(ci) msize(vtiny)),   ///
       name(plot1_female, replace) ///
	   note("") /// 
       legend(pos(6) ring(0) region(lstyle(none)) col(4) colgap(1pt) keygap(1pt) order(1 "Homes" 2 "Settlement" 3 "Justified" 4 "Means") size(16pt)) ///
       ytitle("Prop. Supporting Hate Crime", height(6)) ylab(.0 .1 .2 .3) xlab(1 "Male" 2 "Female") ///
       xtitle("Gender")
	   
** age_group
twoway (scatter pol_message_age_group_est pol_message_age_group, msymbol(0) mcolor(gs0))   ///
	   (scatter prevent_settlement_age_group_est prevent_settlement_age_group, msymbol(Oh) mcolor(red))  ///
	   (scatter justified_age_group_est justified_age_group, msymbol(S) mcolor(gs0))  ///
	   (scatter violence_means_age_group_est violence_means_age_group, msymbol(Sh) mcolor(red))  ///
	   (rcap pol_message_age_group_UL pol_message_age_group_LL pol_message_age_group, lstyle(ci) msize(vtiny))  ///
	   (rcap prevent_settlement_age_group_UL prevent_settlement_age_group_LL prevent_settlement_age_group, lstyle(ci) msize(vtiny))   ///
       (rcap justified_age_group_UL justified_age_group_LL justified_age_group, lstyle(ci) msize(vtiny))   ///
	   (rcap violence_means_age_group_UL violence_means_age_group_LL violence_means_age_group, lstyle(ci) msize(vtiny)),   ///
       name(plot2_age, replace) ///
	   note("") /// 
       legend(off) note("") ///
       ytitle(" ", height(6)) ylab(.0 .1 .2 .3) xlab(1 "18-29" 2 "30-39" 3 "40-49" 4 "50-59" 5 "60+") ///
       xtitle("Age Group")
	   
** income
twoway (scatter pol_message_income_est pol_message_income, msymbol(0) mcolor(gs0))   ///
	   (scatter prevent_settlement_income_est prevent_settlement_income, msymbol(Oh) mcolor(red))  ///
	   (scatter justified_income_est justified_income, msymbol(S) mcolor(gs0))  ///
	   (scatter violence_means_income_est violence_means_income, msymbol(Sh) mcolor(red))  ///
	   (rcap pol_message_income_UL pol_message_income_LL pol_message_income, lstyle(ci) msize(vtiny))  ///
	   (rcap prevent_settlement_income_UL prevent_settlement_income_LL prevent_settlement_income, lstyle(ci) msize(vtiny))   ///
       (rcap justified_income_UL justified_income_LL justified_income, lstyle(ci) msize(vtiny))   ///
	   (rcap violence_means_income_UL violence_means_income_LL violence_means_income, lstyle(ci) msize(vtiny)),   ///
       name(plot3_income, replace) ///
	   legend(off) note("") ///
       ytitle(" ", height(6)) ylab(.0 .1 .2 .3) ///
       xtitle("Household Income (Quintiles)") 

** education
twoway (scatter pol_message_edu_est pol_message_edu, msymbol(0) mcolor(gs0))   ///
	   (scatter prevent_settlement_edu_est prevent_settlement_edu, msymbol(Oh) mcolor(red))  ///
	   (scatter justified_edu_est justified_edu, msymbol(S) mcolor(gs0))  ///
	   (scatter violence_means_edu_est violence_means_edu, msymbol(Sh) mcolor(red))  ///
	   (rcap pol_message_edu_UL pol_message_edu_LL pol_message_edu, lstyle(ci) msize(vtiny))  ///
	   (rcap prevent_settlement_edu_UL prevent_settlement_edu_LL prevent_settlement_edu, lstyle(ci) msize(vtiny))   ///
       (rcap justified_edu_UL justified_edu_LL justified_edu, lstyle(ci) msize(vtiny))   ///
	   (rcap violence_means_edu_UL violence_means_edu_LL violence_means_edu, lstyle(ci) msize(vtiny)),   ///
       name(plot4_edu, replace) ///
	   legend(off) note("")  /// 
       ytitle("Prop. Supporting Hate Crime", height(6)) ylab(.0 .1 .2 .3) ///
       xtitle("Years of Education (Quintiles)") 

** education, young males
twoway (scatter pol_message_edu_ym_est pol_message_edu_ym, msymbol(0) mcolor(gs0))   ///
	   (scatter prevent_settlement_edu_ym_est prevent_settlement_edu_ym, msymbol(Oh) mcolor(red))  ///
	   (scatter justified_edu_ym_est justified_edu_ym, msymbol(S) mcolor(gs0))  ///
	   (scatter violence_means_edu_ym_est violence_means_edu_ym, msymbol(Sh) mcolor(red))  ///
	   (rcap pol_message_edu_ym_UL pol_message_edu_ym_LL pol_message_edu_ym, lstyle(ci) msize(vtiny))  ///
	   (rcap prevent_settlement_edu_ym_UL prevent_settlement_edu_ym_LL prevent_settlement_edu_ym, lstyle(ci) msize(vtiny))   ///
       (rcap justified_edu_ym_UL justified_edu_ym_LL justified_edu_ym, lstyle(ci) msize(vtiny))   ///
	   (rcap violence_means_edu_ym_UL violence_means_edu_ym_LL violence_means_edu_ym, lstyle(ci) msize(vtiny)),   ///
       name(plot5_edu_ym, replace) ///
	   legend(off) note("")  /// 
       ytitle(" ", height(6)) ylab(.0 .1 .2 .3 .4 .5) ///
       xtitle("Years of Education (Quintiles), Men Aged 18-39") 
	   
** education, young females
twoway (scatter pol_message_edu_yf_est pol_message_edu_yf, msymbol(0) mcolor(gs0))   ///
	   (scatter prevent_settlement_edu_yf_est prevent_settlement_edu_yf, msymbol(Oh) mcolor(red))  ///
	   (scatter justified_edu_yf_est justified_edu_yf, msymbol(S) mcolor(gs0))  ///
	   (scatter violence_means_edu_yf_est violence_means_edu_yf, msymbol(Sh) mcolor(red))  ///
	   (rcap pol_message_edu_yf_UL pol_message_edu_yf_LL pol_message_edu_yf, lstyle(ci) msize(vtiny))  ///
	   (rcap prevent_settlement_edu_yf_UL prevent_settlement_edu_yf_LL prevent_settlement_edu_yf, lstyle(ci) msize(vtiny))   ///
       (rcap justified_edu_yf_UL justified_edu_yf_LL justified_edu_yf, lstyle(ci) msize(vtiny))   ///
	   (rcap violence_means_edu_yf_UL violence_means_edu_yf_LL violence_means_edu_yf, lstyle(ci) msize(vtiny)),   ///
       name(plot6_edu_yf, replace) ///
       legend(off) note("") ///
       ytitle(" ", height(6)) ylab(.0 .1 .2 .3 .4 .5) ///
       xtitle("Years of Education (Quintiles), Women Aged 18-39")  
	   
graph dir plot*
graph combine `r(list)', col(3) name(demographics_support_w4_6groups, replace) note("") xsize(14) ysize(8)	   
graph export "Results/Figure_3.pdf", replace



*********************************************************************************
***** Table 2: Effect of Candidates Endorsing Violence on Candidate Support *****
*********************************************************************************
use "Data/for_dataverse.dta", clear
keep if focus == 1
keep if wave==4 

* store independent variables for regression in global macro
global conjafdvars "i.afdv1_fac i.afdv2_fac ib2.afdv3_fac i.afdv4_fac ib2.afdv5_fac i.afdv6_fac ib3.afdv7_fac"
global conjcduvars "i.cduv1_fac i.cduv2_fac ib2.cduv3_fac i.cduv4_fac ib2.cduv5_fac i.cduv6_fac ib2.cduv7_fac"

* loop across parties
local party "afd" "cdu"
foreach v in "own_party" "all" {
	global voters = "`v'"
	foreach p in "`party'" {
		* set restriction to own-party voters, if applicable
		if "$voters" == "own_party" {
			global first_condition = "(`p'_first==1 | `p'_second==1) &"
		}
		else {
			global first_condition = ""
		}
		* perform analysis
		eststo clear

		** full sample
		if "$voters" == "own_party"  {
			qui reg choice`p' ${conj`p'vars} if (`p'_first==1 | `p'_second==1), vce(cluster pseudonym)
			eststo main_`p'_full
		} 
		else {
			qui reg choice`p' ${conj`p'vars}, vce(cluster pseudonym)
			eststo main_`p'_full		
		}

		** hate_pol_message
		foreach i in 0 1   {
			qui reg choice`p' ${conj`p'vars} if ${first_condition} dum_hate_pol_message == `i', vce(cluster pseudonym)
			eststo  main_`p'`i'_mess
		}

		** hate_justified 
		foreach i in 0 1   {
			qui reg choice`p' ${conj`p'vars} if ${first_condition} dum_hate_justified == `i', vce(cluster pseudonym)
			eststo main_`p'`i'_just
		}

		** hate_violence
		foreach i in 0 1 {
			qui reg choice`p' ${conj`p'vars} if ${first_condition} dum_hate_violence == `i', vce(cluster pseudonym)
			eststo  main_`p'`i'_viol	
		}

		** hate_prevent
		foreach i in 0 1   {
			qui reg choice`p' ${conj`p'vars} if ${first_condition} dum_hate_prevent == `i', vce(cluster pseudonym)
			eststo  main_`p'`i'_prev
		}

		esttab main* using "Results/Table_2_`p'_${voters}.tex", mtitles keep(1.`p'v5_fac) se replace
	}
}
